package clientserver;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.ResultSetMetaData;


/*
 * 
 */
/**
 * This is serialized and more simple  version of ReSultSet class, wgich could be passed from server to client via socket connection
 * @author Maxim Rahman
 *
 */
public class QueryResult implements  java.io.Serializable
 

{

	ArrayList<String> rows = null; // a vector containing the row vectors
	 private  ArrayList<String> Columnnames = null;
;

	
	
	private int     columnsinresult;
	private int currentrow;  // for iteration via QueryResult rows
	private int   rowsinresult;
	
	private static final long serialVersionUID = -7475647446554633714L;

	
	// simple reset function
	public void  reset()
	{
		rows=Columnnames=null;
				columnsinresult=currentrow=rowsinresult=0;
				
	}
	
	
	/*Constructor receives not serializable ResultSet object and build from him
	 *  a serializable QueryResult Object, which could be transferred from Server to Client.
	 */
	
	
	public  QueryResult( ResultSet d) throws Exception
	{
		
		ResultSet data=d;
		 String answ=""; 
		ResultSetMetaData rsmd = null;
		

	
		 rsmd=(ResultSetMetaData) data.getMetaData();
		 columnsinresult=rsmd.getColumnCount();
		
		rowsinresult=0;
		 Columnnames = new ArrayList<String>();
			
		 
		 // save all column meta information we needed: column names
		for(int i = 1; i<=columnsinresult; i++)
	      {
			String cname=rsmd.getColumnName(i);

			Columnnames.add(cname);
			
	      }

		 rows = new ArrayList<String>();
		 ArrayList<Object> rowlist =new ArrayList<Object>();
		 while(d.next())
	 		{
	
			 
			
				
		for( int i=1;i<=columnsinresult;i++)
				{

						answ=answ+d.getObject(i)+":";
				}

		
			
		 rows.add(answ);
	    	answ="";
	    	rowsinresult++;
	 		}
		
		 
	
			currentrow=0;
			
	 
		 
	}  // constructor end 
	
	
	
//Info functions//////////////////	
	public   int rowsize()
	{
		return rowsinresult;
	}
	
	public   int columnsize()
	{
		return columnsinresult;
	}
	
	// mooves if it's possible to next row( returns true) otherwise returns false
 public boolean next()
 
 {
	 if(currentrow<columnsinresult)
	 {
		 currentrow++;
		 return true;
	 } else return false;
		 
 }
//Get functions//////////////////
	public  String  getString( String columnname)
	{

		
		if(currentrow<rowsinresult )
			{
			int  colindex=Columnnames.indexOf(columnname); 
			
				String row=rows.get(currentrow);   
			
			String answer[]=row.split(":");
			String a=answer[colindex];
			
			return a;
			} else return null;
	
	
	}

	public  int  getint( String columnname)
	{

			int  colindex=Columnnames.indexOf(columnname); 
		
			
				String row=rows.get(currentrow);   

			String answer[]=row.split(":");

			 String t=answer[colindex];

			 int a=Integer.parseInt(t);
			 return a;
		
	
	
	}
	
	
	
	public void p()
	{
System.out.println(Columnnames);
System.out.println(rows);

	}

	
}
